import math

def maxfont(lines):
    M=0
    for line in lines:
        s = sum(line) + len(line) - 1
        if(s>M):
            M = s
    M = min(W/M,H/len(lines))
    
    if(M<1):
        return 0
    else:
        return math.floor(M)


def findmax(x,y):
    M=0
    if(len(x)>0):
        M = maxfont(x+[y])
    else:
        M = maxfont([y])
        
    for i in range(1,len(y)):
        r = findmax(x + [y[:i]], y[i:])
        if(r>M):
            M = r
            
    return M
            
    
f_in = open('input.txt', 'r')

N = int(f_in.readline().rstrip())

for i in range(1,N+1):
    (W,H, text) = f_in.readline().split(' ',2)
    (W,H) = (int(W),int(H))
    words = [len(x) for x in text.rstrip().split()]
    
    print(i, findmax([],words))

f_in.close();